﻿package tv.colmeia.game.comm {
		/**
	* ...
	* @author Martin Fabichak @ colmeia.tv
	*/
	
	import flash.errors.*;
	import flash.events.*;
	import flash.net.Socket;
	import flash.net.XMLSocket;
	import tv.colmeia.game.assets.events.GameSocketEvent;

	public class GameSocket extends XMLSocket {
		private var response:String;
		private var player1:String;
		private var player2:String;
		
		public function GameSocket(host:String = null, port:uint = 0) {
			super();
			configureListeners();
			connect(host, port);
		}

		private function configureListeners():void {
			trace( "configureListeners : " + configureListeners );
			addEventListener(Event.CLOSE, closeHandler);
			addEventListener(Event.CONNECT, connectHandler);
			addEventListener(DataEvent.DATA, dataHandler);
			addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
		}
		private function dataHandler(event:DataEvent):void {
			trace(event.data);
			var s:String = event.data;
			s.replace("\n", "");
			readResponse(s);
		}
		private function sendRequest():void {
			trace("sendRequest");
			response = "";
		}

		private function readResponse(str:String):void {
			var mac:String = str.substring(0, 17);
			trace( "mac : " + mac );
			var botao:String;
			
			//var botao2:int = str.substring(, 1) as int;
			//trace( "botao2 : " + botao2 );
			
			var a:Array = str.split(",");
			for( var i:String in a ) trace( "key : " + i + ", value : " + a[ i ] );
			
			botao = a[1].toString();
			trace( "botao : " + botao );
			if (!player1) {
				trace( "player1 : " + player1 );
				player1 = mac;
			}else if (!player2 && player1 != mac) {
				trace( "player2 : " + player2 );
				player2 = mac;
			}
			
			if (mac == player1) {
				trace( "mac == player1 : " + mac == player1 );
				if (botao == "4")	dispatchEvent(new GameSocketEvent(GameSocketEvent.GAME_EVENT, 65));
				if (botao == "6")	dispatchEvent(new GameSocketEvent(GameSocketEvent.GAME_EVENT, 68));
				
			}
			if (mac == player2) {
				trace( "mac == player2 : " + mac == player2 );
				if (botao == "4")	dispatchEvent(new GameSocketEvent(GameSocketEvent.GAME_EVENT, 37));
				if (botao == "6")	dispatchEvent(new GameSocketEvent(GameSocketEvent.GAME_EVENT, 39));
			}
		}

		private function closeHandler(event:Event):void {
			trace("closeHandler: " + event.type);
		}

		private function connectHandler(event:Event):void {
			trace("connectHandler: " + event.type);
			//sendRequest();
		}

		private function ioErrorHandler(event:IOErrorEvent):void {
			trace("ioErrorHandler: " + event);
		}

		private function securityErrorHandler(event:SecurityErrorEvent):void {
			trace("securityErrorHandler: " + event);
		}

		private function socketDataHandler(event:ProgressEvent):void {
			/*trace("socketDataHandler: " + event.bytesTotal +  "  "+ event.toString() );*/
		}
	}
	
}